package com.qf.service.impl;

import cn.hutool.core.date.DateTime;
import com.auth0.jwt.JWT;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.entity.TlBed;
import com.qf.entity.TlHotel;
import com.qf.entity.TlHotelOrder;
import com.qf.entity.TlUser;
import com.qf.mapper.BedMapper;
import com.qf.mapper.HotelMapper;
import com.qf.mapper.HotelOrderMapper;
import com.qf.mapper.UserMapper;
import com.qf.service.HotelOrderService;
import com.qf.utils.Mycode;
import com.qf.utils.RedisKey;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
 public class HotelOrderServiceImpl extends ServiceImpl<HotelOrderMapper, TlHotelOrder> implements HotelOrderService {
    @Autowired
    HotelMapper hotelMapper;
    @Autowired
    BedMapper bedMapper;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    UserMapper userMapper;
    /**
     *
     * @param uuid 用户全查订单
     * @return 该用户的所有订单
     */
    @Override
    public ResultMsg findUserHotelOrder(String uuid) {
        String o = (String) redisTemplate.boundValueOps(RedisKey.LOGIN_USER+uuid).get();
        List<String> audience = JWT.decode(o).getAudience();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_name",audience.get(0));
        TlUser tlUser = userMapper.selectOne(queryWrapper);
        queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",tlUser.getUserId());
        List<TlHotelOrder> list = super.list(queryWrapper);
        return ResultMsg.SUCCESS(list);
    }

    /**
     * 再订单表中删除已经支付的订单
     * @param hid 已支付的订单编号
     * @return
     */
    @Override
    public ResultMsg deletepayorder(String hid) {
        boolean b = super.removeById(hid);
        if (!b)
            return ResultMsg.ERROR(Mycode.HOTRELORDER_DEL);
        return ResultMsg.SUCCESS();
    }


    /**
     *
     * @param tlHotelOrder 修改后的订单信息
     * @return
     */
    @Override
    public ResultMsg update(TlHotelOrder tlHotelOrder) {
        boolean b = super.saveOrUpdate(tlHotelOrder);
        if (b)
            return  ResultMsg.SUCCESS();
        return ResultMsg.ERROR(Mycode.HOTELORDER_UPDATE);
    }

    /**
     *
     * @param hotelId
     * @param bedname
     * @param Price
     * @param request
     * @return
     */
    @Override
    public ResultMsg insertHotelOrder(Integer hotelId, String bedname, BigDecimal Price, HttpServletRequest request) {
        String token = request.getHeader("token");
        String o = (String) redisTemplate.boundValueOps(RedisKey.LOGIN_USER+token).get();
        List<String> audience = JWT.decode(o).getAudience();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_name",audience.get(0));
        TlUser tlUser = userMapper.selectOne(queryWrapper);

        TlHotelOrder tlHotelOrder=new TlHotelOrder();
        TlHotel tlHotel = hotelMapper.selectById(hotelId);
        String orderId = UUID.randomUUID().toString();
        tlHotelOrder.setHorderId(orderId);
        tlHotelOrder.setHorderFlag(0);
        tlHotelOrder.setHotelId(hotelId);
        tlHotelOrder.setHorderName(tlHotel.getHotelName());
        tlHotelOrder.setUserId(tlUser.getUserId());
        tlHotelOrder.setHorderBname(bedname);
        tlHotelOrder.setHorderUname(tlUser.getUserName());
        tlHotelOrder.setHorderTel(tlUser.getUserTel());
        tlHotelOrder.setHorderTime(new DateTime());
        tlHotelOrder.setHorderPrice(Price);
        boolean save = super.save(tlHotelOrder);
        if (save) {

            TlBed bed = bedMapper.findByhotelIdandbedName(hotelId, bedname);
            bed.setBedNum(bed.getBedNum()-1);
            int i = bedMapper.updateById(bed);
            return ResultMsg.SUCCESS();
        }
        return ResultMsg.ERROR(Mycode.HOTELORDERINSERT_ERRO);
    }

    /**
     * 查询单独订单的详情
     * @param horderId 该订单的订单编号
     * @return
     */
    @Override
    public ResultMsg findOneOrder(String horderId) {
        TlHotelOrder byId = super.getById(horderId);
        return ResultMsg.SUCCESS(byId);
    }

    /**
     * 取消预订的酒店订单
     * @param horderId 该订单ID
     * @return
     */
    @Override
    public ResultMsg deletenopayorder(String horderId) {//取消预订的酒店订单
        TlHotelOrder byId = super.getById(horderId);
        Integer hotelId = byId.getHotelId();//酒店的ID
        boolean b = super.removeById(horderId);
        if (!b)
            return ResultMsg.ERROR(Mycode.HOTRELORDER_DEL);

        String horderBname = byId.getHorderBname();//酒店下房型的名称
        TlBed bed = bedMapper.findByhotelIdandbedName(hotelId, horderBname);//查询该房型信息
        bed.setBedNum(bed.getBedNum()+1);//该房型数量加1
        int i = bedMapper.updateById(bed);//修改数量
        return ResultMsg.SUCCESS();
    }

    @Override//退款
    public ResultMsg tuikuan(String horderId) {
        TlHotelOrder byId = super.getById(horderId);
        byId.setHorderFlag(2);
        boolean b = super.saveOrUpdate(byId);
        if (b) {
            String bedname = byId.getHorderBname();
            Integer hotelId = byId.getHotelId();
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("hotel_id",hotelId);
            queryWrapper.eq("bed_name",bedname);
            TlBed tlBed = bedMapper.selectOne(queryWrapper);
            tlBed.setBedNum(tlBed.getBedNum()+1);
            bedMapper.updateById(tlBed);
            return ResultMsg.SUCCESS();
        }
        return ResultMsg.ERROR(Mycode.UPDATE_ERRO);
    }

    @Override
    public ResultMsg zero(String uuid) {
        List six = this.six(uuid, 0);
        return ResultMsg.SUCCESS(six);
    }
    @Override
    public ResultMsg one(String uuid) {
        List six = this.six(uuid, 1);
        return ResultMsg.SUCCESS(six);
    }
    @Override
    public ResultMsg two(String uuid) {
        List six = this.six(uuid, 2);
        return ResultMsg.SUCCESS(six);
    }

    public List six(String uuid,Integer num) {
        String o = (String) redisTemplate.boundValueOps(RedisKey.LOGIN_USER+uuid).get();
        List<String> audience = JWT.decode(o).getAudience();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_name",audience.get(0));
        TlUser tlUser = userMapper.selectOne(queryWrapper);
        queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",tlUser.getUserId());
        queryWrapper.eq("horder_flag",num);
        List list = super.list(queryWrapper);
        return list;
    }


}
